home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1999 #1 / Amiga Plus 1999 #1.iso / System-Boost / Workbench / WinTool / WinTool.c < prev    next >
C/C++ Source or Header  |  1998-06-28  |  10KB  |  376 lines

  1. // WinTool.c by Fredrik Söderberg
  2. // Any questions, suggestions, anything, email me.
  3. // ///S and ///E means fold, S for Start and E for End (GoldEd)
  4.  
  5. ///S includes
  6. #include <exec/types.h>
  7. #include <exec/memory.h>
  8. #include <intuition/intuition.h>
  9. #include <intuition/gadgetclass.h>
  10. #include <intuition/IntuitionBase.h>
  11. #include <libraries/gadtools.h>
  12. #include <clib/exec_protos.h>
  13. #include <clib/intuition_protos.h>
  14. #include <clib/gadtools_protos.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. ///E
  18.  
  19. ///S Defines for gadgets
  20. #define TITLEG     1
  21. #define WIDTHG     2
  22. #define HEIGHTG    3
  23. #define LEFTEDGEG  4
  24. #define TOPEDGEG   5
  25. #define MOUSEXG    6
  26. #define MOUSEYG    7
  27. #define CLOSEG     8
  28. #define FORCEG     9
  29. #define ZIPG      10
  30. #define EXECUTEG  11
  31. #define SAFEG     12
  32. ///E
  33.  
  34. ///S Global
  35. struct IntuitionBase *IntuitionBase;
  36. struct Library *GadToolsBase;
  37. struct Window *wtwin;
  38.  
  39. struct TextAttr topaz8 = {"topaz.font", 8, 0, 0};
  40. struct Gadget *titleg, *widthg, *heightg, *topg, *leftg, *mxg, *myg, *closeg;
  41. struct Gadget *forceg, *zipg, *executeg, *safeg;
  42. struct Gadget *gadgetlist=NULL;
  43. ///E
  44.  
  45. ///S prototypes
  46. void killwin(struct Window *);
  47. void executewin(struct Window *, int );
  48. unsigned int getwinstat(struct Window *);
  49. void eventloop(void);
  50. struct Window *createwin(struct Screen *,struct VisualInfo *);
  51. int main(void);
  52. ///E
  53.  
  54.  
  55.  
  56. ///S main(void)
  57. int main(void)
  58. {
  59.    struct Screen *wtscreen;
  60.    struct VisualInfo *wtvi;
  61.    
  62.    if(IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 37L))
  63.    {
  64.       if(GadToolsBase = OpenLibrary("gadtools.library", 37L))
  65.       {
  66.          if(wtscreen = LockPubScreen(NULL))
  67.          {
  68.             if(wtvi = GetVisualInfo(wtscreen, TAG_END))
  69.             {
  70.                if(wtwin = createwin(wtscreen, wtvi))
  71.                {
  72.                   GT_RefreshWindow(wtwin, NULL);
  73.                   eventloop();
  74.                   CloseWindow(wtwin);
  75.                   if(gadgetlist) FreeGadgets(gadgetlist);
  76.                }
  77.                else puts("Could not open buttons");
  78.                FreeVisualInfo(wtvi);
  79.             }
  80.             else puts("Could not open VisualInfo");
  81.             UnlockPubScreen(NULL, wtscreen);
  82.          }
  83.          else puts("Could not LockPubScreen");
  84.          CloseLibrary(GadToolsBase);
  85.       }
  86.       else puts("Could not open gadtools.library V37");
  87.    }
  88.    else puts("Could not open intuition.library V37");
  89.  
  90. return 0;
  91.  
  92. }     
  93. ///E
  94.  
  95. ///S executewin(win, title, tlen)
  96. void executewin(struct Window *win, int tlen)
  97. {
  98.    LONG le=0, te=0, w=0, h=0;
  99.    LONG czip=FALSE, cclear=FALSE, cforce=FALSE, csafe=FALSE;
  100.    char *title;
  101.  
  102.    GT_GetGadgetAttrs(titleg, wtwin,NULL,
  103.       GTST_String, &title, TAG_END);
  104.    if(strlen(title)!=tlen)
  105.       title[tlen]='\0';
  106.    strcpy(win->Title, title);
  107.  
  108.    GT_GetGadgetAttrs(leftg, wtwin,NULL,
  109.       GTIN_Number, &le, TAG_END);
  110.    GT_GetGadgetAttrs(topg, wtwin,NULL,
  111.       GTIN_Number, &te, TAG_END);
  112.    GT_GetGadgetAttrs(widthg, wtwin,NULL,
  113.       GTIN_Number, &w, TAG_END);
  114.    GT_GetGadgetAttrs(heightg, wtwin,NULL,
  115.       GTIN_Number, &h, TAG_END);
  116.    GT_GetGadgetAttrs(zipg, wtwin,NULL,
  117.       GTCB_Checked, &czip, TAG_END);
  118.    GT_GetGadgetAttrs(closeg, wtwin,NULL,
  119.       GTCB_Checked, &cclear, TAG_END);
  120.    GT_GetGadgetAttrs(forceg, wtwin,NULL,
  121.       GTCB_Checked, &cforce, TAG_END);
  122.    GT_GetGadgetAttrs(safeg, wtwin,NULL,
  123.       GTCB_Checked, &csafe, TAG_END);
  124.  
  125.    SetWindowTitles(win, win->Title, (UBYTE *) ~0);
  126.  
  127.    if(csafe)
  128.    {
  129.       win->MinWidth=1;
  130.       win->MinHeight=1;
  131.    }
  132.    ChangeWindowBox(win, le, te, w, h);
  133.  
  134.    if(czip)
  135.       ZipWindow(win);
  136.    if(cclear)
  137.       killwin(win);
  138.    if(cforce)
  139.       CloseWindow(win);
  140.    getwinstat(win);
  141. }
  142. ///E
  143.  
  144. ///S getwinstat(win)
  145. unsigned int getwinstat(struct Window *win)
  146. {
  147.    GT_SetGadgetAttrs(titleg, wtwin,NULL,
  148.       GTST_String, win->Title, TAG_END);
  149.    GT_SetGadgetAttrs(widthg, wtwin,NULL,
  150.       GTIN_Number, win->Width, TAG_END);
  151.    GT_SetGadgetAttrs(heightg, wtwin,NULL,
  152.       GTIN_Number, win->Height, TAG_END);
  153.    GT_SetGadgetAttrs(topg, wtwin,NULL,
  154.       GTIN_Number, win->TopEdge, TAG_END);
  155.    GT_SetGadgetAttrs(leftg, wtwin,NULL,
  156.       GTIN_Number, win->LeftEdge, TAG_END);
  157.    GT_SetGadgetAttrs(mxg, wtwin,NULL,
  158.       GTIN_Number, win->MouseX, TAG_END);
  159.    GT_SetGadgetAttrs(myg, wtwin,NULL,
  160.       GTIN_Number, win->MouseY, TAG_END);
  161.    return(strlen(win->Title));
  162. }
  163. ///E
  164.  
  165. ///S eventloop(void)
  166. void eventloop(void)
  167. {
  168.    struct IntuiMessage *intmess;
  169.    struct Gadget *gad;
  170.    int going = TRUE, tlen=0;
  171.    ULONG class, code;
  172.    struct Window *win;
  173.  
  174.    win = wtwin;
  175.    while(going)
  176.    {
  177.       WaitPort(wtwin->UserPort);
  178.       while((intmess = GT_GetIMsg(wtwin->UserPort)) && going)
  179.       {
  180.          class = intmess->Class;
  181.          code = intmess->Code;
  182.          gad = (struct Gadget *) intmess->IAddress;
  183.          GT_ReplyIMsg(intmess);
  184.          switch(class)
  185.          {
  186.             case IDCMP_GADGETUP:
  187.                if(gad->GadgetID == EXECUTEG)
  188.                {
  189.                   executewin(win, tlen);
  190.                   tlen = getwinstat(win);
  191.                }
  192.                break;
  193.  
  194.             case IDCMP_INACTIVEWINDOW:
  195.                {
  196.                   win = IntuitionBase->ActiveWindow;
  197.                   tlen = getwinstat(win);
  198.                }
  199.                break;
  200.  
  201.             case IDCMP_CLOSEWINDOW:
  202.                going = FALSE;
  203.                break;
  204.  
  205.             case VANILLAKEY:
  206.                if(code==27)
  207.                   going = FALSE;
  208.                break;
  209.  
  210.             case IDCMP_REFRESHWINDOW:
  211.                GT_BeginRefresh(wtwin);
  212.                GT_EndRefresh(wtwin, TRUE);
  213.                break;
  214.          }
  215.       }
  216.    }
  217. }
  218. ///E
  219.  
  220. ///S win = createwin(screen, vi, gadgetlist)
  221. struct Window *createwin(struct Screen *screen, struct VisualInfo *vi)
  222. {
  223.    struct NewGadget newgadget;
  224.    struct Window *win;
  225.  
  226.    titleg = CreateContext(&gadgetlist);
  227.  
  228.    newgadget.ng_TextAttr = &topaz8;
  229.    newgadget.ng_VisualInfo = vi;
  230.    newgadget.ng_LeftEdge = 84;
  231.    newgadget.ng_TopEdge = 15;
  232.    newgadget.ng_Width = 250;
  233.    newgadget.ng_Height = 12;
  234.    newgadget.ng_GadgetText = "Title";
  235.    newgadget.ng_GadgetID = TITLEG;
  236.    newgadget.ng_Flags = 0;
  237.    titleg = CreateGadget(STRING_KIND, titleg, &newgadget,
  238.       GTST_MaxChars, 255, TAG_END);
  239.  
  240.    newgadget.ng_TopEdge += 15;
  241.    newgadget.ng_Width = 40;
  242.    newgadget.ng_GadgetText = "Width";
  243.    newgadget.ng_GadgetID = WIDTHG;
  244.    widthg = CreateGadget(INTEGER_KIND, titleg, &newgadget,
  245.       TAG_END);
  246.  
  247.    newgadget.ng_TopEdge += 15;
  248.    newgadget.ng_GadgetText = "Height";
  249.    newgadget.ng_GadgetID = HEIGHTG;
  250.    heightg = CreateGadget(INTEGER_KIND, widthg, &newgadget,
  251.       TAG_END);
  252.  
  253.    newgadget.ng_TopEdge += 15;
  254.    newgadget.ng_GadgetText = "LeftEdge";
  255.    newgadget.ng_GadgetID = LEFTEDGEG;
  256.    leftg = CreateGadget(INTEGER_KIND, heightg, &newgadget,
  257.       TAG_END);
  258.  
  259.    newgadget.ng_TopEdge += 15;
  260.    newgadget.ng_GadgetText = "TopEdge";
  261.    newgadget.ng_GadgetID = TOPEDGEG;
  262.    topg = CreateGadget(INTEGER_KIND, leftg, &newgadget,
  263.       TAG_END);
  264.  
  265.    newgadget.ng_TopEdge += 15;
  266.    newgadget.ng_GadgetText = "MouseX";
  267.    newgadget.ng_GadgetID = MOUSEXG;
  268.    mxg = CreateGadget(INTEGER_KIND, topg, &newgadget,
  269.       TAG_END);
  270.  
  271.    newgadget.ng_LeftEdge += 120;
  272.    newgadget.ng_GadgetText = "MouseY";
  273.    newgadget.ng_GadgetID = MOUSEYG;
  274.    myg = CreateGadget(INTEGER_KIND, mxg, &newgadget,
  275.       TAG_END);
  276.  
  277.    newgadget.ng_LeftEdge = 245;
  278.    newgadget.ng_TopEdge = 42;
  279.    newgadget.ng_GadgetText = "Close Window";
  280.    newgadget.ng_GadgetID = CLOSEG;
  281.    closeg = CreateGadget(CHECKBOX_KIND, myg, &newgadget, TAG_END);
  282.  
  283.    newgadget.ng_LeftEdge += 35;
  284.    newgadget.ng_Flags = PLACETEXT_ABOVE;
  285.    newgadget.ng_GadgetText = "With Force";
  286.    newgadget.ng_GadgetID = FORCEG;
  287.    forceg = CreateGadget(CHECKBOX_KIND, closeg, &newgadget, TAG_END);
  288.  
  289.    newgadget.ng_LeftEdge = 245;
  290.    newgadget.ng_TopEdge += 15;
  291.    newgadget.ng_Flags = PLACETEXT_LEFT;
  292.    newgadget.ng_GadgetText = "Zip Window";
  293.    newgadget.ng_GadgetID = ZIPG;
  294.    zipg = CreateGadget(CHECKBOX_KIND, forceg, &newgadget, TAG_END);
  295.  
  296.    newgadget.ng_LeftEdge = 280;
  297.    newgadget.ng_TopEdge += 15;
  298.    newgadget.ng_Flags = PLACETEXT_LEFT;
  299.    newgadget.ng_GadgetText = "Remove size limits";
  300.    newgadget.ng_GadgetID = SAFEG;
  301.    safeg = CreateGadget(CHECKBOX_KIND, zipg, &newgadget, TAG_END);
  302.  
  303.    newgadget.ng_LeftEdge = 260;
  304.    newgadget.ng_TopEdge = 90;
  305.    newgadget.ng_Width = 100;
  306.    newgadget.ng_Flags = PLACETEXT_IN;
  307.    newgadget.ng_GadgetText = "Execute";
  308.    newgadget.ng_GadgetID = EXECUTEG;
  309.    executeg = CreateGadget(BUTTON_KIND, safeg, &newgadget, TAG_END);
  310.  
  311.    if(executeg)
  312.    {
  313.       if(!(win = OpenWindowTags(NULL,
  314.          WA_Gadgets, gadgetlist,
  315.          WA_Width, 380,
  316.          WA_Height, 110,
  317.          WA_DragBar, TRUE,
  318.          WA_DepthGadget, TRUE,
  319.          WA_CloseGadget, TRUE,
  320.          WA_Activate, TRUE ,
  321.          WA_IDCMP,    IDCMP_REFRESHWINDOW |
  322.                       IDCMP_CLOSEWINDOW |
  323.                       CHECKBOXIDCMP |
  324.                       INACTIVEWINDOW |
  325.                       VANILLAKEY,
  326.          WA_PubScreen, screen,
  327.          WA_Title, "WinTool",
  328.          WA_ScreenTitle, "WinTool 1.11 by Fredrik Söderberg. (virus free:)",
  329.          TAG_END)))
  330.       {
  331.          puts("Could not open window");
  332.          FreeGadgets(gadgetlist);
  333.          gadgetlist=FALSE;
  334.          return(FALSE);
  335.       }
  336.    }
  337.    else
  338.       return(FALSE);
  339.    return(win);
  340. }
  341. ///E
  342.  
  343. ///S killwin(win)
  344. // Sends a IDCMP_CLOSEWINDOW to the window win
  345. void killwin(struct Window *win)
  346. {
  347.    struct IntuiMessage msg;
  348.    struct MsgPort *port;
  349.    if(!(port = CreateMsgPort()))
  350.    {
  351.        Permit();
  352.        return;
  353.    }
  354.    msg.ExecMessage.mn_Node.ln_Succ = NULL;
  355.    msg.ExecMessage.mn_Node.ln_Pred = NULL;
  356.    msg.ExecMessage.mn_Node.ln_Type = NT_MESSAGE;
  357.    msg.ExecMessage.mn_Node.ln_Name = NULL;
  358.    msg.ExecMessage.mn_Node.ln_Pri  = 0;
  359.    msg.ExecMessage.mn_ReplyPort = port;
  360.    msg.ExecMessage.mn_Length    = sizeof(struct IntuiMessage);
  361.    msg.Class       = IDCMP_CLOSEWINDOW;
  362.    msg.Code        = 0;
  363.    msg.Qualifier   = 0;
  364.    msg.IAddress    = NULL;
  365.    msg.MouseX      = 0;
  366.    msg.MouseY      = 0;
  367.    msg.Seconds     = 0;
  368.    msg.Micros      = 0;
  369.    msg.IDCMPWindow = win;
  370.    msg.SpecialLink = NULL;
  371.    PutMsg(win->UserPort, (struct Message *)&msg);
  372.    WaitPort(port);
  373.    DeleteMsgPort(port);
  374. }
  375. ///E
  376.